$(document).ready(function(){
	$('a.delete-student').on('click',function(e){
		e.preventDefault();

        $( "#msgDialog" ).on( "dialogbeforeclose", function( event, ui ) {
            $("#msgDialog > div.content").empty();
        } );
        
		var url = $(this).attr('href');
		var stud = $(this).attr('data-student-name');
		var linkid = $(this).attr('id');
        
		$("#msgDialog").dialog('option','width',400);
		$("#msgDialog").dialog('option','title','Recycle Bin.');
        $("#msgDialog").dialog('option', 'buttons', {
        "Cancel" : function() {
				$(this).dialog("close");
            }
        });

        var form = '';
        	form += '<form id="delete_student_form" action="'+ url +'" method="post">';
        	form += '<label class="label label-inverse">Reason for Delete</label>';
        	form += '<textarea name="reason_for_delete" style="min-width:350px;max-width:350px;" required></textarea>';
        	form += '<br>';
        	form += '<input type="submit" name="delete" value="Delete Student" class="btn btn-danger">';
        	form += '</form>';
        $("#msgDialog > div.content").html(form);
        $("#msgDialog > p").html("Move <span class='label'>"+ stud +"</span> to BIN?");
        $("#msgDialog").dialog("open");

		
        $(document).on('submit','form#delete_student_form',function(e){
        	e.preventDefault();

        	$.ajax({
        		method:'POST',
        		url:url,
        		data:$(this).serialize()
        	}).done(function(e){
        		if(e == 1)
        		{
        			$('tr.'+linkid).hide(1200);
                    $.blockUI({ 
                        message: 'Moved '+ stud +' to Recycle Bin', 
                        fadeIn: 700, 
                        fadeOut: 2000, 
                        timeout: 4000, 
                        showOverlay: false, 
                        centerY: false, 
                        css: { 
                            width: '350px', 
                            top: '60px', 
                            left: '', 
                            right: '10px', 
                            border: 'none', 
                            padding: '5px', 
                            backgroundColor: '#000', 
                            '-webkit-border-radius': '10px', 
                            '-moz-border-radius': '10px', 
                            opacity: .6, 
                            color: '#fff' 
                        } 
                    }); 
        		}else if(e == 2)
        		{
                    $.blockUI({ 
                        message: 'Cannot delete Already paid student.', 
                        fadeIn: 700, 
                        fadeOut: 700, 
                        timeout: 4000, 
                        showOverlay: false, 
                        centerY: false, 
                        css: { 
                            width: '350px', 
                            top: '60px', 
                            left: '', 
                            right: '10px', 
                            border: 'none', 
                            padding: '5px', 
                            backgroundColor: '#f00', 
                            '-webkit-border-radius': '10px', 
                            '-moz-border-radius': '10px', 
                            opacity: .6, 
                            color: '#fff' 
                        } 
                    }); 
        		}else if(e == 0){
        			
        		}
        	}).fail(function(){
                    $.blockUI({ 
                        message: 'Unable to move '+ stud +' to Recycle Bin', 
                        fadeIn: 700, 
                        fadeOut: 700, 
                        timeout: 4000, 
                        showOverlay: false, 
                        centerY: false, 
                        css: { 
                            width: '350px', 
                            top: '60px', 
                            left: '', 
                            right: '10px', 
                            border: 'none', 
                            padding: '5px', 
                            backgroundColor: '#f00', 
                            '-webkit-border-radius': '10px', 
                            '-moz-border-radius': '10px', 
                            opacity: .6, 
                            color: '#fff' 
                        } 
                    });
        	}).always(function(){
                $("#msgDialog").dialog("close");
            });
        });
});

});